//
//  SNFootSettingViewController.m
//  project
//
//  Created by 黄建华 on 2018/11/8.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#define FOODUNITSET @"FOODUNITSET"

#import "SNFoodSettingViewController.h"
#import "TXHRrettyRuler.h"
#import "TargetPickerView.h"
#import "FinishDayPickerView.h"
#import "SNFoodViewController.h"
#import "FinishDayPickerView.h"

@interface SNFoodSettingViewController () <TXHRrettyRulerDelegate>

@property (nonatomic, assign) float          currentWeight;
@property (nonatomic, assign) float          currentWeightTarget;
@property (nonatomic, strong) UILabel        *showLabel;
@property (nonatomic, strong) UILabel        *showLabel2;
@property (nonatomic, strong) TXHRrettyRuler *ruler;
@property (nonatomic, strong) TXHRrettyRuler *ruler2;
@property (nonatomic, assign) NSInteger      weight1;
@property (nonatomic, assign) NSInteger      weight2;
@property (nonatomic, strong) UILabel        *bmiLabel;
@property (nonatomic, strong) UILabel        *bmiLabelInfo;
@property (nonatomic, strong) UILabel        *stepTargetLabel;
@property (nonatomic, strong) UILabel        *calorTargetLabel;
@property (nonatomic, assign) NSInteger      calorTarget;
@property (nonatomic, assign) NSInteger      stepTarget;
@property (nonatomic, strong) SNShadowView   *infoView;
@property (nonatomic, assign) CGRect         infoViewRect;
@property (nonatomic, strong) NSMutableArray *stringArray;
@property (nonatomic, strong) UILabel        *finishDayLabel;
@property (nonatomic, strong) UILabel        *label1;
@property (nonatomic, strong) UILabel        *label2;
@property (nonatomic, strong) UILabel        *label3;
@property (nonatomic, strong) UILabel        *label4;
@property (nonatomic, strong) UILabel        *label5;
@property (nonatomic, strong) UILabel        *label6;
@property (nonatomic, assign) BOOL           unit;
@property (nonatomic, assign) BOOL           first;

@end

@implementation SNFoodSettingViewController

- (NSInteger)getBmr
{
    NSInteger bmr = 0;
    if (SN_USER.gender ==1) {
        bmr = 10*_weight1 +6.25*SN_USER.height - 5*SN_USER.age +5;
    }
    else
    {
        bmr = 10*_weight1 +6.25*SN_USER.height - 5*SN_USER.age -161;
    }
    bmr = bmr/100*100;
    return bmr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.leftButton.hidden = NO;
    [self.leftButton setImageNormal:@"进入"];
    self.sbTitleLable.text = SN_Text(@"我的理想体重");
    
    [self getWeightUnitSet];
//    SNShadowView *shadowLine = [[SNShadowView alloc] init];
//    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
//    [shadowLine showWithType:1];
//    [self.view addSubview:shadowLine];
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
    [self.view addSubview:shadowLine];
    
    NSArray *segmentedArray = [NSArray arrayWithObjects:SN_Text(@"kg"), SN_Text(@"lb"), nil];
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame                = CGRectMake(HHHWIDTH -120, shadowLine.bottom + 8, 110, 30);
    segmentedControl.selectedSegmentIndex = _unit;
    segmentedControl.tintColor            = [UIColor blackColor];
    [segmentedControl addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    
    CGFloat height2 = FitSimpleSize(10, 10, 0, 0, 0);
    
    UILabel *info1 = [[UILabel alloc]initWithFrame:SNFRAME(0, shadowLine.bottom+10, HHHWIDTH, 20)];
    info1.textAlignment = NSTextAlignmentCenter;
    info1.text          = SN_Text(@"当前体重");
    info1.font = SNFONT(SNFITFONTSIZE(15));
    [self.view addSubview:info1];
    
    _showLabel               = [[UILabel alloc] init];
    _showLabel.font          = SNFONT(SNFITFONTSIZE(20));
    _showLabel.frame         = CGRectMake(0, info1.bottom +5 -height2/2.0, HHHWIDTH, 25);
    _showLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_showLabel];
    
    UILabel *info2 = [[UILabel alloc]initWithFrame:SNFRAME(0, _showLabel.bottom + 70 -height2, HHHWIDTH, 20)];
    info2.textAlignment = NSTextAlignmentCenter;
    info2.text          = SN_Text(@"目标体重");
    info2.font = SNFONT(SNFITFONTSIZE(15));
    [self.view addSubview:info2];
    
    _showLabel2               = [[UILabel alloc] init];
    _showLabel2.font          = SNFONT(SNFITFONTSIZE(20));
    _showLabel2.frame         = CGRectMake(0, info2.bottom +5 -height2/2.0, HHHWIDTH, 25);
    _showLabel2.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_showLabel2];
    
    _showLabel.textColor  = FOODCOLOR;
    _showLabel2.textColor = FOODCOLOR;
    _weight1              = SN_USER.weight;
    _weight2              = SN_USER.goal_weight;
    if (_weight2 == 0) {
        _weight2 = _weight1;
    }
    SN_PublicClass.weightSet = _weight1;
    _stepTarget = SN_USER.target_step;
    _calorTarget = SN_USER.goal_calory;
    if (_calorTarget == 0) {
        _calorTarget = [self getBmr];
    }
    [self initTXHRrettyRuler];
    
    UIButton *saveButton = [[UIButton alloc] initWithFrame:SNFRAME(20, HHHHEIGHT - 55 -FitSimpleIpx(0,20), HHHWIDTH - 40, 40)];
    saveButton.backgroundColor = [UIColor blackColor];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [saveButton addCornerRadius:5.0 color:[UIColor blackColor] lineWidth:1];
    saveButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    if (SN_USER.goal_weight == 0) {
        [saveButton setTitle:SN_Text(@"开始制定我的饮食计划") forState:UIControlStateNormal];
    } else {
        [saveButton setTitle:SN_Text(@"保存设置") forState:UIControlStateNormal];
    }
    [saveButton addTarget:self action:@selector(endAndSave) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveButton];
    
    UILabel *bmiInfoLabel = [UILabel simpleWithRect:SNFRAME(0, _ruler2.bottom-15 +FitSimpleIpx(0,30), HHHWIDTH/2.0, 20) withAlignment:NSTextAlignmentRight withFontSize:13.0 withText:SN_Text(@"目标BMI") withTextColor:[UIColor grayColor]];
    bmiInfoLabel.font = SNFITFONT(14);
    [self.view addSubview:bmiInfoLabel];
    
    _bmiLabel = [UILabel simpleWithRect:SNFRAME(HHHWIDTH/2.0 +12, _ruler2.bottom-18 + FitSimpleIpx(0,30),50, 20) withAlignment:NSTextAlignmentLeft withFontSize:13.0 withText:@"24" withTextColor:[UIColor blackColor]];
    _bmiLabel.font = SNFITFONT(20);
    [self.view addSubview:_bmiLabel];
    
    _bmiLabelInfo = [UILabel simpleWithRect:SNFRAME(_bmiLabel.right+3, _ruler2.bottom-15 +FitSimpleIpx(0,30),HHHWIDTH - _bmiLabel.right -5, 20) withAlignment:NSTextAlignmentLeft withFontSize:13.0 withText:@"" withTextColor:[UIColor grayColor]];
    _bmiLabelInfo.font = SNFITFONT(14);
    [_bmiLabelInfo setAdjustsFontSizeToFitWidth:YES];
    [self.view addSubview:_bmiLabelInfo];
    [self BMIUpdate];
    
      /************每日摄入热量设置****************/
    UIButton *calorTarget = [[UIButton alloc]initWithFrame:SNFRAME(20, bmiInfoLabel.bottom+3 +FitSimpleIpx(0,20), HHHWIDTH - 40, 30)];
    [calorTarget addTarget:self action:@selector(calorTargetClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:calorTarget];
    
    UILabel *calorInfo = [UILabel simpleWithRect:SNFRAME(0,5, calorTarget.width *0.75, 20) withAlignment:NSTextAlignmentLeft withFontSize:13.0 withText:SN_Text(@"每日摄入热量设置") withTextColor:[UIColor blackColor]];
    calorInfo.font = SNFITFONT(15);
    calorInfo.adjustsFontSizeToFitWidth = YES;
    [calorTarget addSubview:calorInfo];
    
    _calorTargetLabel      = [UILabel simpleWithRect:SNFRAME(calorTarget.width *0.75,calorInfo.top, calorTarget.width *0.20, 20) withAlignment:NSTextAlignmentRight withFontSize:13.0 withText:[NSString stringWithFormat:@"%ld", (long)_calorTarget] withTextColor:[UIColor blackColor]];
    _calorTargetLabel.font = SNFITFONT(15);
    [calorTarget addSubview:_calorTargetLabel];
    
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(_calorTargetLabel.right+3, calorInfo.top, 22, 22)];
    [imageView2 setImage:UIImageNamed(@"进入(1)")];
    [calorTarget addSubview:imageView2];
    
    /**************当前每日步数目标**************/
    UIButton *stepTarget = [[UIButton alloc]initWithFrame:SNFRAME(20, calorTarget.bottom+5+FitSimpleIpx(0,10), HHHWIDTH - 40, 30)];
    [stepTarget addTarget:self action:@selector(stepTargetClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stepTarget];
    
    UILabel *stepInfo = [UILabel simpleWithRect:SNFRAME(0,5, stepTarget.width *0.75, 20) withAlignment:NSTextAlignmentLeft withFontSize:13.0 withText:SN_Text(@"当前每日步数目标") withTextColor:[UIColor blackColor]];
    stepInfo.font = SNFITFONT(15);
    [stepTarget addSubview:stepInfo];
    
    _stepTargetLabel      = [UILabel simpleWithRect:SNFRAME(stepTarget.width *0.75,stepInfo.top, stepTarget.width *0.20, 20) withAlignment:NSTextAlignmentRight withFontSize:13.0 withText:[NSString stringWithFormat:@"%ld", (long)_stepTarget] withTextColor:[UIColor blackColor]];
    _stepTargetLabel.font = SNFITFONT(15);
    [stepTarget addSubview:_stepTargetLabel];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_stepTargetLabel.right+3, stepInfo.top, 22, 22)];
    [imageView setImage:UIImageNamed(@"进入(1)")];
    [stepTarget addSubview:imageView];
    
    CGFloat height = saveButton.top - stepTarget.bottom;
    CGFloat ipx = FitSimpleIpx(1.0, 0.75);
    _infoViewRect = SNFRAME(20, stepTarget.bottom +5 +FitSimpleIpx(0,30), HHHWIDTH-40, ipx*height-10);
    [self infoViewUpdate];
}

// 更新计划
- (void)updateInfoLabel {
    _stringArray = [NSMutableArray array];
    NSString  *targetWeightString = @"";
    NSInteger targetWeight        = _weight1 - _weight2;
    if (_unit) {
        targetWeightString = [NSString stringWithFormat:@"%ld lb", (long)targetWeight];
    } else {
        targetWeightString = [NSString stringWithFormat:@"%ld kg", (long)targetWeight];
    }
    [_stringArray addObject:targetWeightString];
    [_stringArray addObject:[NSString stringWithFormat:@"%ld %@", _stepTarget, SN_Text(@"步/天")]];
    [_stringArray addObject:@""];
    [_stringArray addObject:@""];
    [_stringArray addObject:@""];
    [_stringArray addObject:@""];
    
    //运动消耗
    NSInteger sportCalor = (int)(_weight1*2 *1.036 * (SN_USER.height *0.45 *_stepTarget *0.00001));
    NSInteger needCalor  = 26*_weight1;
    // 摄入食物
    NSInteger BMR = SN_USER.goal_calory;
    if (BMR == 0) {
        BMR = [self getBmr];
    }
    // 消化食物也要能量 为基础代谢的10%
    NSInteger foodCalor  = 0;
    NSInteger totalCalor = sportCalor +needCalor +foodCalor;
    // 只有totalCalor > Bmr的时候才能减肥 反之则增肥
    // 每日总消耗
    NSInteger reduceWeight = totalCalor - BMR;
    reduceWeight = 1.0*reduceWeight /7.776;
    // 需要的天数(大于0才能减肥 小于0表示应调整参数)
    NSInteger needDays = targetWeight*1000/reduceWeight;
    
    for (int i = 0; i < _stringArray.count; i++) {
        if (i == 0) {
            if (targetWeight <= 0) {
                _label1.textColor = [UIColor redColor];
                _label1.text      = SN_Text(@"目标体重应小于当前体重");
            } else {
                _label1.textColor = [UIColor grayColor];
                NSString                  *text = [NSString stringWithFormat:@"%@ %@", SN_Text(@"您的目标是减去"), [_stringArray objectAtIndex:i]];
                NSMutableAttributedString *str  = [self AttributedStringWith:text changeText:[_stringArray objectAtIndex:i]];
                _label1.text           = text;
                _label1.attributedText = str;
            }
        }
        if (i == 1) {
            NSString                  *text = [NSString stringWithFormat:@"%@ %@", SN_Text(@"当前每天运动量约为"), [_stringArray objectAtIndex:i]];
            NSMutableAttributedString *str  = [self AttributedStringWith:text changeText:[_stringArray objectAtIndex:i]];
            _label2.text           = text;
            _label2.attributedText = str;
        }
        if (i == 2) {
            NSString *chagneText1 = [NSString stringWithFormat:@"%ld %@", (long)totalCalor, SN_Text(@"大卡")];
            NSString *chagneText2 = [NSString stringWithFormat:@"%ld %@", (long)BMR, SN_Text(@"大卡")];
            _label3.text = [NSString stringWithFormat:@"%@ %@,%@%@", SN_Text(@"每日消耗"), chagneText1, SN_Text(@"每日摄入"), chagneText2];
            [_label3 setAttributedText:[self label1With:_label3.text withFontsize:SNFITFONTSIZE(15) withChangeText1:chagneText1 withChangeText2:chagneText2]];
        }
        if (i == 3) {
            if (reduceWeight > 0) {
                NSString *changeT = @"";
                if (_unit) {
                    changeT = [NSString stringWithFormat:@"%.2f lb", 1.0*reduceWeight/1000*2.2];
                } else {
                    changeT = [NSString stringWithFormat:@"%.2f kg", 1.0*reduceWeight/1000];
                }
                _label4.text           = [NSString stringWithFormat:@"%@ %@", SN_Text(@"当前预计每天减少约"), changeT];
                _label4.attributedText = [self AttributedStringWith:_label4.text changeText:changeT];
            } else {
                _label4.text = [NSString stringWithFormat:@"%@", SN_Text(@"当前计划不适宜减肥")];
            }
        }
        if (i == 4) {
            if (reduceWeight > 0.0 && targetWeight > 0) {
                NSString *changeT = [NSString stringWithFormat:@"%d %@", needDays, SN_Text(@"天")];
                _label5.text           = [NSString stringWithFormat:@"%@ %@,%@", SN_Text(@"完成大概需"), changeT, SN_Text(@"预计成功时间为")];
                _label5.attributedText = [self AttributedStringWith:_label5.text changeText:changeT];
            } else {
                _label5.text = @"";
            }
        }
        if (i == 5) {
            _label6.font = SNFONT(SNFITFONTSIZE(22));
            if (reduceWeight > 0.0 && targetWeight > 0) {
                _label6.textColor = [UIColor blackColor];
                NSDate   *finishDays = [[NSDate date] dateAfterDay:needDays];
                NSString *dateString = [finishDays dateToDayString];
                _label6.text = dateString;
            } else {
                _label6.textColor = [UIColor redColor];
                _label6.text      = SN_Text(@"请调整计划");
            }
        }
    }
}

// 设置多段字体 参数1 字号  参数2 改变文字2 参数3 改变文字2
- (NSMutableAttributedString *)label1With:(NSString *)text withFontsize:(CGFloat)fontsize withChangeText1:(NSString *)text1 withChangeText2:(NSString *)text2 {
    NSMutableAttributedString *noteStr;
    
    if (!text) {
        return noteStr;
    }
    noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (text1.length > 0) {
        NSUInteger first1 = [[noteStr string] rangeOfString:text1].location;
        // 需要改变的区间
        NSRange range1 = NSMakeRange(first1, text1.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:fontsize] range:range1];
        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range1];
    }
    
    if (text2.length > 0) {
        NSUInteger first2 = [[noteStr string] rangeOfString:text2].location;
        // 需要改变的区间
        NSRange range2 = NSMakeRange(first2, text2.length);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:fontsize] range:range2];
        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range2];
    }
    
    return noteStr;
}

- (NSMutableAttributedString *)AttributedStringWith:(NSString *)text changeText:(NSString *)changeText {
    NSMutableAttributedString *str;
    
    if (!text) {
        return str;
    }
    str = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range1 = [[str string] rangeOfString:changeText];
    [str addAttribute:NSFontAttributeName value:SNFONT(SNFITFONTSIZE(15)) range:range1];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range1];
    return str;
}

- (void)infoViewUpdate {
    _infoView                   = [[SNShadowView alloc] init];
    _infoView.frame             = _infoViewRect;
    _infoView.layer.borderWidth = 0.5;
    _infoView.layer.borderColor = SNCOLORGRAYLINE.CGColor;
    [self.view addSubview:_infoView];
    
    CGFloat labelHeight = _infoView.height *0.8/6;
    for (int i = 0; i < 6; i++) {
        UILabel *infoLabel = [[UILabel alloc] init];
        infoLabel.frame         = SNFRAME(0, 5 + i *labelHeight, _infoView.width, labelHeight);
        infoLabel.textAlignment = NSTextAlignmentCenter;
        infoLabel.textColor     = [UIColor grayColor];
        infoLabel.font          = SNFONT(SNFITFONTSIZE(14));
        infoLabel.text          = StrByInt(i);
        infoLabel.adjustsFontSizeToFitWidth = YES;
        if (i == 0) {
            _label1 = infoLabel;
            [_infoView addSubview:_label1];
        }
        if (i == 1) {
            _label2 = infoLabel;
            [_infoView addSubview:_label2];
        }
        if (i == 2) {
            _label3 = infoLabel;
            [_infoView addSubview:_label3];
        }
        if (i == 3) {
            _label4 = infoLabel;
            [_infoView addSubview:_label4];
        }
        if (i == 4) {
            _label5 = infoLabel;
            [_infoView addSubview:_label5];
        }
        if (i == 5) {
            _label6 = infoLabel;
            [_infoView addSubview:_label6];
        }
    }
    
    UILabel *underlineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, _infoView.height *0.8, _infoView.width, _infoView.height*0.2))];
    underlineLabel.textAlignment = NSTextAlignmentCenter;
    underlineLabel.font          = SNFONT(SNFITFONTSIZE(14));
    NSString *textStr = SN_Text(@"这是什么？");
    // 下划线
    NSDictionary              *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName : FOODCOLOR};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];
    
    //赋值
    underlineLabel.attributedText = attribtStr;
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelClick)];
    [underlineLabel addGestureRecognizer:gestureRecognizer];
    underlineLabel.userInteractionEnabled = YES;
    [_infoView addSubview:underlineLabel];
    [self updateInfoLabel];
}

- (void)labelClick {
    HDAlertView *alertView = [HDAlertView alertViewWithTitle:SN_Text(@"说明") andMessage:SN_Text(@"食谱解释")];
    [alertView addButtonWithTitle:SN_Text(@"确定") type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
    }];
    [alertView show];
}

- (void)showTargetPickerView {
    DEF_WEAKSELF_(SNFoodSettingViewController)
    TargetPickerView *setView = [[TargetPickerView alloc]initWithFrame:self.view.bounds ];
    [self.view addSubview:setView];
    setView.target = _stepTarget;
    [setView animationUp];
    setView.sureButtonBlock = ^(NSString *str) {
        weakSelf.stepTargetLabel.text = str;
        _stepTarget                   = str.integerValue;
        [weakSelf updateInfoLabel];
    };
}

- (void)stepTargetClick {
    [self showTargetPickerView];
}

- (void)calorTargetClick
{
    DEF_WEAKSELF_(SNFoodSettingViewController)
    FinishDayPickerView *setView = [[FinishDayPickerView alloc]initWithFrame:self.view.bounds ];
    [self.view addSubview:setView];
    NSInteger target = SN_PublicClass.todayFood.goal_calory;
    if (target == 0) {
        target = [self getBmr];
    }
    setView.weight = target;
    [setView animationUp];
    setView.sureButtonBlock = ^(NSString *str, NSString *timeStr) {
        weakSelf.calorTargetLabel.text = str;
        SN_USER.goal_calory = str.integerValue;
        SN_PublicClass.todayFood.goal_calory = SN_USER.goal_calory;
        
        [SN_Net showHUD:SN_Text(@"请等待...") withTime:5.0];
        
        [SN_Net updateUserInfoWithSuccess:^(id object) {
            SHOWMBProgressHUD(nil, SN_Text(@"目标保存成功"), nil, NO, 2.0);
            [SN_PublicClass.todayFood updateToDB];
            [SN_USER updateToDB];
            [weakSelf updateInfoLabel];
            HIDDENMBProgressHUD;
        } fail:^(id object) {
            SHOWMBProgressHUD(nil, SN_Text(@"目标保存失败"), nil, NO, 2.0);
            HIDDENMBProgressHUD;
        }];
    };
}

- (void)BMIUpdate {
    CGFloat BMIValue = _weight2*1.0/(SN_USER.height*1.00/100*SN_USER.height*1.00/100);
    _bmiLabel.text = [NSString stringWithFormat:@"%.1f", BMIValue];
    if (BMIValue >24.0) {
        _bmiLabel.textColor = FOODCOLOR;
    }
    else
    {
        _bmiLabel.textColor = [UIColor blackColor];
    }
    NSString *string = @"";
    if (BMIValue <= 18.5) {
        string = SN_Text(@"过轻");
    }else if(BMIValue <= 24)
    {
    string = SN_Text(@"正常");
    }
    else if(BMIValue <= 27)
    {
        string = SN_Text(@"过重");
    }
    else if(BMIValue <= 32)
    {
        string = SN_Text(@"肥胖");
    }
    else
    {
        string = SN_Text(@"非常肥胖");
    }
    _bmiLabelInfo.text = string;
}

- (BOOL)getWeightUnitSet {
    _unit = [FOODUNITSET getBOOLValue];
    return _unit;
}

- (void)initTXHRrettyRuler {
    if (_ruler) {
        [_ruler removeFromSuperview];
        _ruler = nil;
    }
    _ruler               = [[TXHRrettyRuler alloc] initWithFrame:CGRectMake(0, _showLabel.bottom +5, HHHWIDTH, 70)];
    _ruler.rulerDeletate = self;
    _ruler.unitSet       = _unit;
    _ruler.tag           = 1;
    [_ruler showRulerScrollViewWithCount:120 average:[NSNumber numberWithFloat:1.0] currentValue:1.0*(_weight1-30) smallMode:YES];
    [self.view addSubview:_ruler];
    
    
    if (_ruler2) {
        [_ruler2 removeFromSuperview];
        _ruler2 = nil;
    }
    _ruler2               = [[TXHRrettyRuler alloc] initWithFrame:CGRectMake(0, _showLabel2.bottom +5, HHHWIDTH, 70)];
    _ruler2.rulerDeletate = self;
    _ruler2.unitSet       = _unit;
    _ruler2.tag           = 2;
    [_ruler2 showRulerScrollViewWithCount:120 average:[NSNumber numberWithFloat:1.0] currentValue:1.0*(_weight2-30) smallMode:YES];
    [self.view addSubview:_ruler2];
    
    if (_unit) {
        _showLabel.text  = [NSString stringWithFormat:@"%.0f lb", 2.2*_weight1];
        _showLabel2.text = [NSString stringWithFormat:@"%.0f lb", 2.2*_weight2];
    } else {
        _showLabel.text  = [NSString stringWithFormat:@"%.0f kg", 1.0*_weight1];
        _showLabel2.text = [NSString stringWithFormat:@"%.0f kg", 1.0*_weight2];
    }
}

- (void)selectItem:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        NSLog(@"kg");
        [FOODUNITSET setBOOLValue:NO];
    } else {
        NSLog(@"lb");
        [FOODUNITSET setBOOLValue:YES];
    }
    [self getWeightUnitSet];
    [self initTXHRrettyRuler];
    [self updateInfoLabel];
}

- (void)txhRrettyRuler:(TXHRulerScrollView *)rulerScrollView {
    if (rulerScrollView.tag == 1) {
        if (_unit) {
            _showLabel.text = [NSString stringWithFormat:@"%.0f lb", (rulerScrollView.rulerValue +30.0)*2.2];
        } else {
            _showLabel.text = [NSString stringWithFormat:@"%.0f kg", rulerScrollView.rulerValue +30.0];
        }
        _weight1 = rulerScrollView.rulerValue +30.0;
        SN_PublicClass.weightSet = _weight1;
        
        [self updateInfoLabel];
    }
    if (rulerScrollView.tag == 2) {
        if (_unit) {
            _showLabel2.text = [NSString stringWithFormat:@"%.0f lb", (rulerScrollView.rulerValue +30.0)*2.2];
        } else {
            _showLabel2.text = [NSString stringWithFormat:@"%.0f kg", rulerScrollView.rulerValue +30.0];
        }
        _weight2 = rulerScrollView.rulerValue +30.0;
        [self BMIUpdate];
        [self updateInfoLabel];
    }
}

- (void)functionSetLeft {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)functionSetRight {
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _first = NO;
    if (SN_USER.goal_weight == 0) {
        SHOWMBProgressHUD(nil, SN_Text(@"第一次使用需要设置相关设置"), nil, NO, 2.0);
        _first = YES;
    }
}

- (void)endAndSave {
    if (_weight1 <= _weight2) {
        SHOWMBProgressHUD(nil, SN_Text(@"目标体重应小于当前体重"), nil, NO, 2.0);
    } else {
        SN_USER.weight      = _weight1;
        SN_USER.goal_weight = _weight2;
        SN_USER.target_step = _stepTarget;
        
        SN_PublicClass.todayFood.weight = _weight1;
        SN_PublicClass.todayFood.goal_weight = _weight2;
        
        [SN_Net updateUserInfoWithSuccess:^(id object) {
            SN_PublicClass.todaySport.stepTarget = _stepTarget;
            if (SN_PublicClass.todaySport.step >= _stepTarget) {
                SN_PublicClass.todaySport.stepquality = YES;
                SN_PublicClass.todaySport.stepBoolFinish = 1;
            } else {
                SN_PublicClass.todaySport.stepquality = NO;
                SN_PublicClass.todaySport.stepBoolFinish = 0;
            }
            
            [SN_USER updateToDB];
            [SN_PublicClass.todayFood updateToDB];
            [SN_PublicClass.todaySport updateToDB];
            
            SHOWMBProgressHUD(nil, SN_Text(@"保存成功"), nil, NO, 2.0);
            [KK_BLESend sendUserInfoSettingWithBlock:^(id object, KKBLEAcceptType type) {
            }];
            
            if (_first) {
                // 如果是第一次 则跳转到添加食谱
                SNFoodViewController *vc = [[SNFoodViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                [self functionSetLeft];
            }
        } fail:^(id object) {
            SHOWMBProgressHUD(nil, SN_Text(@"保存失败"), nil, NO, 2.0);
        }];
    }
}

@end
